/*
 * This class displays the login screen for clerk or admin.
 */

/*
 * Home.java
 *
 * Created on 05-Apr-2011, 21:17:21
 */

package pos.ui.screen;

import pos.auth.Authentication;
import pos.exception.POSException;
import pos.util.Validate;
import pos.interfaces.Messages;
import pos.ui.screen.administrator.AdministratorConsole;
import sun.security.acl.PermissionImpl;

/**
 *
 * @author Parvez
 */
public class Home extends javax.swing.JFrame implements Messages {
    String errorMsg = "";
    String action = null;
    Customer cust;

    /** Creates new form Home */
    public Home(String act) {
        this.action = act;
        setTitle(AuthLoginTitle);
        if(Authentication.isLoggedIn()) {
            this.dispose();
            new Logout();
        } else {
            initComponents();
        }
    }
    
    /** Creates new form Home */
    public Home(String act, Customer cust) {
        this.action = act;
        this.cust = cust;
        setTitle(AuthLoginTitle);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TitleText = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        userNameField = new javax.swing.JTextField();
        message = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        TitleText.setFont(new java.awt.Font("Verdana", 0, 24));
        TitleText.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        TitleText.setText("Digishop POS");

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 18));
        jLabel1.setText("User:");

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 18));
        jLabel2.setText("Password:");

        jButton1.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        jButton1.setText("Submit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        userNameField.setFocusCycleRoot(true);
        userNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userNameFieldActionPerformed(evt);
            }
        });

        message.setFont(new java.awt.Font("Verdana", 0, 10));
        message.setForeground(new java.awt.Color(204, 0, 0));
        message.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        jButton2.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(169, 169, 169)
                        .addComponent(TitleText, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(43, 43, 43)
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
                            .addComponent(message, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(passwordField)
                                    .addComponent(userNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))))))
                .addContainerGap(134, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleText, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(userNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * This method is invoked with the submit button is pressed. It validates the
     * user inputs and authenticates the user
     * 
     * @param evt ActionEvent
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO connect to database search for username and associated password abd verify
        String userName = this.userNameField.getText();
        char[] password = this.passwordField.getPassword();
        String pswd = null;

        Validate val = new Validate();
        
        try {
//            System.out.println(password);
            userName = val.validTextInput(userName);
        } catch(POSException e) {
            this.errorMsg = e.getMessage() + " for user. ";
//            System.out.println(this.errorMsg);
        }

        try {
            pswd = val.validTextInput(new String(password));
        } catch(POSException e) {
            this.errorMsg += e.getMessage() + " for password.";
//            System.out.println(this.errorMsg);
        }

        if(!this.errorMsg.equalsIgnoreCase("")) {
            this.message.setText(this.errorMsg);
            this.message.setVisible(true);
            this.errorMsg = "";
        } else {
            if(Authentication.isValidUser(userName, pswd)) {
                if(Authentication.hasPermission(new PermissionImpl(action))) {
                    if(this.action.equalsIgnoreCase("ADMIN_CONSOLE")) {
                        new AdministratorConsole().setVisible(true);
                    } else if(this.action.equalsIgnoreCase("HELP_CUSTOMER")) {
                        this.cust.jButton7.setEnabled(true);
                        this.cust.discountBtn.setEnabled(true);
                    }
                    this.dispose();
                } else {
                    this.errorMsg = "You do not have sufficient previleges.";
                    this.message.setText(this.errorMsg);
                    this.message.setVisible(true);
                    this.errorMsg = "";
                }
            } else {
                this.errorMsg = "Invalid user or password. Please try again.";
                this.message.setText(this.errorMsg);
                this.message.setVisible(true);
                this.errorMsg = "";
            }
            this.passwordField.setText(null);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void userNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userNameFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_userNameFieldActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
}//GEN-LAST:event_formWindowClosing

    /**
     * This method closes the home screen.
     * 
     * @param evt 
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel TitleText;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel message;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JTextField userNameField;
    // End of variables declaration//GEN-END:variables

}
